package webs.servlet.ServletCookie;
//设置Cookie存储时间

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/servletCookieDemo1")
public class ServletCookieDemo1 extends HttpServlet
{
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
    {
        Cookie cookie = new Cookie("name", "lisi");
        //设置Cooki存活时间
        //1.存储时间正数
        //cookie.setMaxAge(180);
        //2.存储时间为0，默认情况，关闭浏览器，断开连接就销毁Cookie
        //cookie.setMaxAge(0);
        //3.存储时间为负数，删除Cookie
        //cookie.setMaxAge(-1);
        resp.addCookie(cookie);
    }
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
    {
        this.doPost(req,resp);
    }
}
